package GoogleIntegration;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.google.gdata.client.GoogleAuthTokenFactory.UserToken;
import com.google.gdata.client.Query;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class GCalSample { 

    public static void main(String[] args) {
        CalendarService myService = new CalendarService("CS2103-TaskO-1.0");
        try {
			myService.setUserCredentials("adith.g2@gmail.com", "*****");
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        URL feedUrl;
        CalendarFeed resultFeed;
		try {
			feedUrl = new URL("http://www.google.com/calendar/feeds/default/owncalendars/full");
			resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
			
			UserToken auth_token = (UserToken) myService.getAuthTokenFactory().getAuthToken();
			
			//TODO: use the token correctly
			String token = auth_token.getValue();
			
			System.out.println("Calendars you own:");
		        System.out.println();

		        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		          CalendarEntry entry = resultFeed.getEntries().get(i);
		          System.out.println("\t" + entry.getTitle().getPlainText());
		        }
		        
		        System.out.println("\n");
		        
		     // Send the request and receive the response:
		        CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
		        for (int i = 0; i < myFeed.getEntries().size(); i++) {
			          CalendarEventEntry entry = myFeed.getEntries().get(i);
			          System.out.println("\t" + entry.getTitle().getPlainText());
			        }
		        
		        
		        URL postUrl =
		        		  new URL("https://www.google.com/calendar/feeds/adith.g2@gmail.com/private/full");
		        		CalendarEventEntry myEntry = new CalendarEventEntry();

		        		myEntry.setTitle(new PlainTextConstruct("Tennis with Beth"));
		        		myEntry.setContent(new PlainTextConstruct("Meet for a quick lesson."));

		        		DateTime startTime = DateTime.parseDateTime("2012-10-01T08:30:00");
		        		DateTime endTime = DateTime.parseDateTime("2012-10-01T09:30:00");
		        		When eventTimes = new When();
		        		eventTimes.setStartTime(startTime);
		        		eventTimes.setEndTime(endTime);
		        		myEntry.addTime(eventTimes);

		        		// Send the request and receive the response:
		        		CalendarEventEntry insertedEntry = myService.insert(postUrl, myEntry);
		        		
		        		
		        		myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
		        		for (int i = 0; i < myFeed.getEntries().size(); i++) {
					          CalendarEventEntry entry = myFeed.getEntries().get(i);
					          System.out.println("\t" + entry.getTitle().getPlainText());
					        }
		        		
		        		Query myQuery = new Query(postUrl);
		        		myQuery.setFullTextQuery("Tennis");
		        		CalendarEventFeed myResultsFeed = myService.query(myQuery,
		        		    CalendarEventFeed.class);
		        		if (myResultsFeed.getEntries().size() > 0) {
		        		  CalendarEventEntry firstMatchEntry = (CalendarEventEntry)
		        		      myResultsFeed.getEntries().get(0);
		        		  String myEntryTitle = firstMatchEntry.getTitle().getPlainText();
		        		  System.out.println("\n");
		        		  System.out.println(myEntryTitle);
		        		  System.out.println("\n");
		        		  List<When> times = firstMatchEntry.getTimes();
		        		  
		        		  for (When when : times) {
		        			  System.out.println("\n" +when.getStartTime().toString());
		        			  System.out.println(when.getEndTime().toString());
						}
		        		}
		        		

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
    }
}